﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Silverlight.Services;
using System.Windows.Media.Imaging;
using Galaktika.BI.Silverlight.Controls;

namespace Galaktika.BI.Silverlight.ClientApp
{
    /// <summary>
    /// Represents separate layout item.
    /// </summary>
    public class LayoutControlItem : UserControl
    {
        Grid m_Container;
        Grid m_RootContainer;
        private Grid m_Parent;

        public LayoutControlItem(LayoutItemBase layout, Grid parent)
        {
            this.Layout = layout;
            base.Name = layout.Name;

            m_Container = new Grid();
            m_RootContainer = new Grid();
            m_RootContainer.Children.Add(m_Container);
            m_Parent = parent;
            base.Content = m_RootContainer;

            base.Margin = new Thickness(0.0);
        }

        public LayoutItemBase Layout { get; private set; }
        internal RowDefinition Row { get; set; }
        internal ColumnDefinition Column { get; set; }
        //public FrameworkElement Element { get; private set; }

        private void EnsureSizeConstraints(FrameworkElement fe)
        {
            if (fe is ISizeConstraintsRequestor)
            {
                ISizeConstraintsRequestor constraints = (ISizeConstraintsRequestor)fe;
                if (!double.IsNaN(constraints.RequiredMinHeight))
                {
                    if (constraints.RequiredMinHeight > this.Row.MinHeight)
                    {
                        this.Row.MinHeight = constraints.RequiredMinHeight;
                    }
                }
                if (!double.IsNaN(constraints.RequiredMinWidth))
                {
                    if (constraints.RequiredMinWidth > this.Column.MinWidth)
                    {
                        this.Column.MinWidth = constraints.RequiredMinWidth;
                    }
                }
            }
        }

        public void AssignContent(UIElement content, AppearanceGroup appearance)
        {
            FrameworkElement fe = content as FrameworkElement;
            if (fe == null) return;

            var padding = this.Layout.Padding;
            fe.Margin = new Thickness( padding.Left, padding.Top, padding.Width, padding.Height );
            if (this.Layout.IsTextVisible)
            {
                TextBlock text = new TextBlock();
                if (!string.IsNullOrEmpty(appearance.FontFamily))
                {
                    text.FontFamily = new FontFamily(appearance.FontFamily);
                }
                text.TextAlignment = TextAlignment.Left;
                text.VerticalAlignment = VerticalAlignment.Center;
                text.Text = this.Layout.Text;

                double textWidth = this.Layout.TextSize.Width * 1.3;
                switch (this.Layout.TextLocation)
                {
                    case TextLocation.Left:
                        text.Margin = new Thickness(0.0, 0.0, this.Layout.TextToControlDistance, 0.0);
                        m_Container.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(textWidth) });
                        m_Container.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
                        Grid.SetColumn(text, 0);
                        Grid.SetColumn(fe, 1);
                        break;
                    case TextLocation.Top:
                        m_Container.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                        m_Container.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                        Grid.SetRow(text, 0);
                        Grid.SetRow(fe, 1);
                        break;
                    case TextLocation.Right:
                        text.Margin = new Thickness(this.Layout.TextToControlDistance, 0.0, 0.0, 0.0);
                        m_Container.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(textWidth) });
                        m_Container.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
                        Grid.SetColumn(text, 1);
                        Grid.SetColumn(fe, 0);
                        break;
                    case TextLocation.Bottom:
                        m_Container.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                        m_Container.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                        Grid.SetRow(text, 1);
                        Grid.SetRow(fe, 0);
                        break;
                }

                m_Container.Children.Add(text);
                m_Container.Children.Add(content);
            }
            else
            {
                m_Container.Children.Add(content);
            }

            var typeName = fe.GetType().FullName;
            if (typeName.Contains("PivotGridControl") ||
                typeName.Contains("Chart"))
            {
                FullScreenButton btnFullScreen = new FullScreenButton();
                btnFullScreen.Visibility = Visibility.Collapsed;
                btnFullScreen.Width = btnFullScreen.Height = 36.0;
                btnFullScreen.BorderBrush =
                btnFullScreen.Background = new SolidColorBrush(Colors.Transparent);
                btnFullScreen.Content =
                    new Image()
                    {
                        Source = new BitmapImage(new Uri("/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/FullScreen32.png", UriKind.Relative)),
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment = VerticalAlignment.Center
                        ,Margin = new Thickness(2.0)
                    };
                btnFullScreen.VerticalContentAlignment = VerticalAlignment.Center;
                btnFullScreen.HorizontalContentAlignment = HorizontalAlignment.Center;
                btnFullScreen.HorizontalAlignment = HorizontalAlignment.Right;
                btnFullScreen.VerticalAlignment = VerticalAlignment.Top;
                btnFullScreen.Click += (s, e) =>
                {
                    if (FullScreenSwitcher.IsFullScreen)
                    {
                        FullScreenSwitcher.Switch();
                    }
                    else
                    {
                        FullScreenSwitcher.ShowInFullScreen(this);
                    }
                };
                (Parent as UIElement).MouseEnter += (s, e) => 
                {
                    if (FullScreenSwitcher.IsFullScreen)
                    {
                        ToolTipService.SetToolTip(btnFullScreen, Localization.ToolTip_ExitFullscreenMode);
                    }
                    else
                    {
                        ToolTipService.SetToolTip(btnFullScreen, Localization.Tooltip_ViewInFullScreenMode);
                    }

                    btnFullScreen.Visibility = Visibility.Visible;
                    btnFullScreen.MouseEnter -= btnFullScreen_MouseEnter;
                    btnFullScreen.MouseEnter += btnFullScreen_MouseEnter;

                    btnFullScreen.MouseLeave -= btnFullScreen_MouseLeave;
                    btnFullScreen.MouseLeave += btnFullScreen_MouseLeave;
                };
                (Parent as UIElement).MouseLeave += (s, e) =>
                {
                    //if (!FullScreenSwitcher.IsFullScreen)
                    //{
                        btnFullScreen.Visibility = Visibility.Collapsed;
                    //}
                        btnFullScreen.MouseEnter -= btnFullScreen_MouseEnter;
                        btnFullScreen.MouseLeave -= btnFullScreen_MouseLeave;
                };
                m_RootContainer.Children.Add(btnFullScreen);
            }

            this.EnsureSizeConstraints(fe);
        }

        private void btnFullScreen_MouseLeave(object s, EventArgs e)
        {
            Button btn = s as Button;
            if (btn != null)
            {
                Storyboard st = btn.Tag as Storyboard;
                if (st != null)
                {
                    st.Stop();
                }
            }
        }

        private void btnFullScreen_MouseEnter(object s, EventArgs e)
        {
            Button btn = s as Button;
            if (btn != null)
            {
                var st = new Storyboard();
                st.Duration = new Duration(TimeSpan.FromSeconds(2.0));
                st.Completed += (_, __) =>
                {
                    ToolTipService.SetToolTip(btn, null);
                    VisualStateManager.GoToState(btn, "Normal", false);
                    btn.Visibility = Visibility.Collapsed;
                };
                st.Begin();
                btn.Tag = st;
            }
        }
    }
}